package com.rongxun.utils;

import android.text.TextUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * Created by zpf on 2016/7/22.
 */
public class TimeUtils {


    public static String secToTime(long time) {
        String timeStr = null;
        long hour = 0;
        long minute = 0;
        long second = 0;
        if (time <= 0)
            return "00:00:00";
        else {
            minute = time / 60;
            if (minute < 60) {
                second = time % 60;
                timeStr = unitFormat(hour) + ":" + unitFormat(minute) + ":" + unitFormat(second);
            } else {
                hour = minute / 60;
                if (hour > 99)
                    return "99:59:59";
                minute = minute % 60;
                second = time - hour * 3600 - minute * 60;
                timeStr = unitFormat(hour) + ":" + unitFormat(minute) + ":" + unitFormat(second);
            }
        }
        return timeStr;
    }

    public static String unitFormat(long i) {
        String retStr = null;
        if (i >= 0 && i < 10)
            retStr = "0" + Long.toString(i);
        else
            retStr = "" + i;
        return retStr;
    }

    /**
     * 获取前后日期 i为正数 向后推迟i天，负数时向前提前i天
     *
     * @param i
     * @return
     */
    public static String getDate(int i) {
        Calendar cd = Calendar.getInstance();
        cd.add(Calendar.DATE, i);
        Date dat = cd.getTime();
        SimpleDateFormat dformat = new SimpleDateFormat("yyyy_MM_dd");
        return dformat.format(dat);
    }

    public static String getTime(String time){
        SimpleDateFormat sf1 = new SimpleDateFormat("EEE MMM dd hh:mm:ss z yyyy", Locale.ENGLISH);
        Date date = null;
        try {
            date = sf1.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        SimpleDateFormat sf2 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        return sf2.format(date);
    }




    /**
     * 获取当前时间的方法
     */
    public static String getTime() {
        Date mDate = new Date();
        DateFormat mDateFormat = new SimpleDateFormat("yyyy_MM_dd");
        mDate.setTime(System.currentTimeMillis());
        return mDateFormat.format(mDate);
    }

    /**
     * @param date yyyy-MM-dd
     * @param calendarType Calendar.Year or Calendar.MONTH or Calendar.DAY_OF_MONTH
     * @return
     */
    public static int getPointTime(String date, int calendarType) {
        if (!TextUtils.isEmpty(date)) {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            try {
                dateFormat.format(dateFormat.parse(date));
                int result = dateFormat.getCalendar().get(calendarType) + 1;
                if (calendarType == Calendar.MONTH) {
                    result = result + 1;
                }
                return result;
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return -1;
    }

    public static int[] getPointTime(String date) {
        int[] array = new int[3];
        if (!TextUtils.isEmpty(date)) {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy_MM_dd");
            try {
                dateFormat.format(dateFormat.parse(date));
                array[0] = dateFormat.getCalendar().get(Calendar.YEAR);
                array[1] = dateFormat.getCalendar().get(Calendar.MONTH) + 1;
                array[2] = dateFormat.getCalendar().get(Calendar.DAY_OF_MONTH);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return array;
    }
}
